<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="固定资产">
          <j-dict-select-tag  v-decorator="['assetFixedId', {}]" :triggerChange="true" placeholder="请选择固定资产"
                              dictCode="zcgl_asset_fixed,name,id" @change="assetFixedChange"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <j-dict-select-tag  v-decorator="['type', {}]" :triggerChange="true" placeholder="请选择类型"
                              dictCode="asset_fixed_log_type"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="操作时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'recordTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="原单位">
          <j-select-category  v-decorator="['oldCategoryId', {}]"  placeholder="请选择单位" disabled></j-select-category>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="新单位">
          <j-select-category  v-decorator="['nowCategoryId', {}]"  placeholder="请选择单位"></j-select-category>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="借用/归还人">
          <a-input placeholder="请输入借用/归还人" v-decorator="['operationUser', {}]" />
        </a-form-item>
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="备注">
            <a-textarea placeholder="请输入备注" v-decorator="['remark', {}]"  style="height: 80px"/>
          </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JSelectCategory from '../../../components/jeecgbiz/JSelectCategory'
  import { getAction } from '../../../api/manage'

  export default {
    name: "AssetFixedLogModal",
    components:{JSelectCategory},
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/zcgl/assetFixedLog/add",
          edit: "/zcgl/assetFixedLog/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'assetFixedId','type','oldCategoryId','nowCategoryId','status','operationUser'))
		  //时间格式化
          this.form.setFieldsValue({recordTime:this.model.recordTime?moment(this.model.recordTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.recordTime = formData.recordTime?formData.recordTime.format('YYYY-MM-DD HH:mm:ss'):null;
            if(!this.model.id){
              formData.status =0
            }
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },
      assetFixedChange(e){
        getAction("zcgl/assetFixed/queryById?id="+e).then(res=>{
          if(res.success){
            this.form.setFieldsValue({'oldCategoryId':res.result.assetFixedCategoryId})
          }

        })
      }
    }
  }
</script>

<style lang="less" scoped>

</style>